package com.changzhi.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.changzhi.common.annotation.Verify;
import com.changzhi.common.exception.ServiceException;
import com.changzhi.common.util.Result;
import com.changzhi.ldyl.model.ActivityPrize;
import com.changzhi.ldyl.model.dto.LoadDto;
import com.changzhi.mapper.ActivityPrizeMapper;
import com.changzhi.service.RedService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:圣诞节
 * @author: 小落
 * @date: 2021年02月25日
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("red")
public class RedController {

	@Resource
	private RedService redService;
	@Resource
	private ActivityPrizeMapper activityPrizeMapper;

	@Verify(timestamp = false)
	@PostMapping("lastAwardList")
	public Result lastAwardList(@RequestBody LoadDto loadDto){
		return Result.success(redService.lastAwardList(loadDto.getPageSize()));
	}

	@Verify(timestamp = false)
	@PostMapping("myRecordList")
	public Result myRecordList(@RequestBody LoadDto loadDto){
		return Result.success(redService.myRecordList(loadDto));
	}

	@Verify(timestamp = false)
	@PostMapping("luckChannel")
	public Result luckChannel(){
		return Result.success(redService.luckChannel());
	}



	@RequestMapping("test")
	public Result test(){
		return Result.success(lottery(1000, getPrizeList(39,40,51)));
	}

	//抽奖
	private Map<String, Integer> lottery(Integer num, List<ActivityPrize> prizeList){
		if(CollectionUtils.isEmpty(prizeList)){
			throw new ServiceException("未配置奖品");
		}
//		List<ActivityPrize> resultList = new ArrayList<>();
		Map<String, Integer> map = new HashMap<>();
		for (int i = 0; i < num; i++) {
			ActivityPrize prize = randomPrize(prizeList);
//			resultList.add(prize);
			Integer count = map.get(prize.getName());
			if(count == null){
				map.put(prize.getName(), 1);
			} else {
				map.put(prize.getName(), ++count);
			}
		}
		return map;
	}


	/**
	 * 抽奖概率，概率不带小数点
	 * @return
	 */
	public static ActivityPrize randomPrize(List<ActivityPrize> prizeList) {
		int sum = prizeList.stream().mapToInt(t -> t.getRate().intValue()).sum();
		ActivityPrize prize = null;
		int rate = 0;
		int random = RandomUtil.randomInt(0, sum);
		for (ActivityPrize item : prizeList) {
			// 中奖概率
			rate += item.getRate();
			if (random < rate) {
				prize = new ActivityPrize();
				try {
					BeanUtil.copyProperties(item,prize);
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			}
		}
		return prize;
	}

	//获取奖品列表
	private List<ActivityPrize> getPrizeList(Integer activityId, Integer minType, Integer maxType){
		LambdaQueryWrapper<ActivityPrize> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(ActivityPrize::getActivityId, activityId);
		if(minType != null){
			wrapper.ge(ActivityPrize::getType, minType);
		}
		if(maxType != null){
			wrapper.le(ActivityPrize::getType, maxType);
		}
		wrapper.orderByDesc(ActivityPrize::getRate);
		List<ActivityPrize> prizeList = activityPrizeMapper.selectList(wrapper);
		prizeList.stream().forEach(t->t.setRate(t.getRate() * 1000));
		return prizeList;
	}
}
